layui.use(['element', 'jquery', 'layer', 'laytpl','laypage','form','table'], function() {
    var $ = layui.jquery,
        layer = layui.layer,
        laytpl = layui.laytpl,
        laypage=layui.laypage,
        form=layui.form,
        element = layui.element
        table = layui.table;

    $(document).ready(function () {
       /* var data ;
        console.log(localStorage.getItem("token"));
        if (localStorage.getItem("token")!=null){
            var data1= localStorage.getItem("token");
            table.render({
                elem: '#pay_tb'
                ,url:'/pay/findByorderId?orderId='+data1
                ,cols: [[
                    {field:'payid', title:'支付编号', width:110, fixed: 'left',unresize: true,  sort: true}
                    ,{field:'orderId', title:'订单编号', width:90}
                    ,{field:'sn', title:'支付记录号', width:200}
                    ,{field:'paySn', title:'支付平台交易号', width:200,}
                    ,{field:'totalAmount', title:'支付金额', width:90}
                    ,{field:'payment', title:'支付方式', width:120,templet:function () {
                            if('payment'){
                                return '<span>支付宝支付</span>';
                            }
                        }}
                    ,{field:'pstatus', title:'支付状态',width:90,templet: function(res){
                            console.log(res.pstatus);
                            data = res.pstatus;

                            if(res.pstatus!=0){
                                $("#p1").html("已支付");
                                return '<span class="layui-badge layui-bg-blue">已支付</span>';
                            }
                            return '<span class="layui-badge">未支付</span>';
                        }}
                    ,{field:'createTime', title:'创建时间', width:180}
                    ,{field:'payTime', title:'付款时间', width:180}
                    ,{fixed: 'right', title:'操作', toolbar: '#pay_tb_bar', width:160}
                ]]
            });
        }else*/
        table.render({
            elem: '#pay_tb'
            ,url:'/pay/findByuserId'
            ,cols: [[
                {field:'payid', title:'支付编号', width:110, fixed: 'left',unresize: true,  sort: true}
                ,{field:'orderId', title:'订单编号', width:90}
                ,{field:'sn', title:'支付记录号', width:200}
                ,{field:'paySn', title:'支付平台交易号', width:200,}
                ,{field:'totalAmount', title:'支付金额', width:90}
                ,{field:'payment', title:'支付方式', width:120,templet:function () {
                        if('payment'){
                            return '<span>支付宝支付</span>';
                        }
                    }}
                ,{field:'pstatus', title:'支付状态',width:90,templet: function(res){
                        console.log(res.pstatus);
                        data = res.pstatus;

                        if(res.pstatus!=0){
                            $("#p1").html("已支付");
                            return '<span class="layui-badge layui-bg-blue">已支付</span>';
                        }
                        return '<span class="layui-badge">未支付</span>';
                    }}
                ,{field:'createTime', title:'创建时间', width:180}
                ,{field:'payTime', title:'付款时间', width:180}
                ,{fixed: 'right', title:'操作', toolbar: '#pay_tb_bar', width:160}
            ]]
             ,page: 2
             ,limit:10
            ,limits:[5,15,30,60,100]
        });
    });



    //监听行工具事件
    table.on('tool(pay_tb)', function(obj){
        var data = obj.data;
        console.log(data);
       /* if(obj.event === 'del'){
            layer.confirm('真的删除该订单记录么', function(index){
                $.ajax({
                    url:'/user_center/orders/'+data.orderId,
                    type:'delete',
                    dataType:'json',
                    success:function (res) {
                        if(res.code!=0){
                            return layer.msg(res.msg, {icon: 2});
                        }
                        return layer.msg("删除成功", {icon: 1},function () {
                            obj.del();
                            $("#order-items").html("");
                        });
                    },
                    error:function () {
                        return layer.msg("服务器错误,请稍后再试", {icon: 2});
                    }
                });
                layer.close(index);
            });
        } else*/ if(obj.event === 'pay'){
            console.log(data.pstatus);
            if(data.pstatus!=0){
                return layer.msg("你已支付该订单", {icon: 2});
            }else
           // console.log("111111");
            tz();
            function tz() {
                $.ajax({
                    url: '/pay/to_alipay?orderId='+data.orderId,
                    success:function (res) {
                        //console.log(res);
                        //跳转到支付宝支付界面
                        window.location.href = '/pay/to_alipay?orderId='+data.orderId;
                    },
                    error:function () {
                        return layer.msg("服务器错误,请稍后再试", {icon: 2});
                    }
                })

            }

        }
    });


});
